<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2019/8/14
 * Time: 11:33
 */
namespace Modules\Api\Http\Controllers;
use App\Http\Controllers\BasicController;
use App\Lib\ErrorCode;
use App\Lib\Helper;
use App\Lib\OilcardException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Modules\Logic\UserLogic;

class LoginController extends BasicController
{
    /**
     * 密码登录
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function passwordlogin(Request $request)
    {
        try{
            $mobile = $request->input("mobile",'');
            $password = $request->input("password",'');
            if (empty($mobile)){
                throw new OilcardException("'mobile'未赋值");
            }
            if (empty($password)){
                throw new OilcardException("'password'未赋值");
            }
            if (true !== Helper::checkMobile($mobile)){
                throw new OilcardException("手机号格式错误");
            }
            if (32 != strlen($password)){
                throw new OilcardException("密码错误");
            }
            $result = (new UserLogic())->passwordLoginApp($mobile,$password,$this->openid,$this->unionid);
            return $this->success($result);
        }catch (OilcardException $e){
            return $this->fail(ErrorCode::ERROR,$e->getMessage());
        }catch (\Exception $e){
            Log::error($e);
            return $this->fail(ErrorCode::ERROR,"系统错误");
        }
    }

    /**
     * 验证码登录
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function smsLogin(Request $request)
    {
        try{
            $mobile = $request->input("mobile",'');
            $code = $request->input("code",'');
            $inviteId = $request->input("invite_id",0);
            if (empty($mobile)){
                throw new OilcardException("'mobile'未赋值");
            }
            if (false===Helper::checkMobile($mobile)){
                throw new OilcardException("手机号格式错误");
            }
            if (empty($code)){
                throw new OilcardException("'code'未赋值");
            }
            if (strlen($code)!=4 || strlen($code)!=6){
                throw new OilcardException("验证码长度有误");
            }
            $result = (new UserLogic())->smsLogin($mobile,$code,$this->openid,$this->unionid,$inviteId);
            return $this->success($result);
        }catch (OilcardException $e){
            return $this->fail(ErrorCode::ERROR,$e->getMessage());
        }catch (\Exception $e){
            return $this->fail(ErrorCode::ERROR,"系统错误");
        }
    }

    /**
     * 发送验证码
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function sendSms(Request $request){
        try{
            $mobile = $request->input("mobile",'');
            if (empty($mobile)){
                throw new OilcardException("'mobile'未赋值");
            }
            if (!Helper::checkMobile($mobile)){
                throw new OilcardException("手机号格式错误");
            }
            $result = (new UserLogic())->sendSms($mobile);
            return $this->success($result);
        }catch (OilcardException $e){
            return $this->fail(ErrorCode::ERROR,$e->getMessage());
        }catch (\Exception $e){
            return $this->fail(ErrorCode::ERROR,"系统错误");
        }
    }

    /**
     * 获取微信access_token
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getWeixinAccessToken(Request $request)
    {
        try{
            $code = $request->input("code",null);
            if (empty($code)){
                throw new OilcardException("'code'未赋值");
            }
            $result = (new UserLogic())->getAccessToken($code);
            return $this->success($result);
        }catch (OilcardException $e){
            return $this->fail(ErrorCode::ERROR,$e->getMessage());
        }catch (\Exception $e){
            return $this->fail(ErrorCode::ERROR,"系统错误");
        }
    }

    /**
     * 微信登陆
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function wxRegister(Request $request)
    {
        try{
            $openid = $request->input("openid",null);
            $unionid = $request->input("unionid",null);
            if (empty($openid) && empty($unionid)){
                throw new OilcardException("'openid'未赋值和'unionid'未赋值");
            }
            $token = (new UserLogic())->weixinLogin($openid,$unionid);
            if (null==$token){
                throw new OilcardException(ErrorCode::UNBIND_MOBILE,"未绑定手机号");
            }
            return $this->success(['token'=>$token]);
        }catch (OilcardException $e){
            return $this->fail(ErrorCode::ERROR,$e->getMessage());
        }catch (\Exception $e){
            return $this->fail(ErrorCode::ERROR,"系统错误");
        }
    }

    /**
     * 绑定手机号
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function userBindMobile(Request $request)
    {
        try{
            $openid   = $request->input('openid');
            $unionid  = $request->input('unionid');
            $nickname = $request->input('nick_name');
            $avatar   = $request->input('avatar');
            $mobile   = $request->input('mobile');
            $smsCode  = $request->input('code');
            if (false===Helper::checkMobile($mobile)){
                throw new OilcardException("手机号格式错误");
            }
            if (6 != strlen($smsCode) || 4 != strlen($smsCode)){
                throw new OilcardException("验证码错误");
            }
            if (empty($openid) && empty($unionid)) {
                throw new OilcardException('openid和unionid不存在');
            }
            $user = (new UserLogic())->getWxUserInfo($openid, $unionid);
            if ($user && !empty($user->mobile)) {
                return $this->fail(ErrorCode::UNBIND_MOBILE, '该账号已绑定手机号');
            }

            $token = (new UserLogic())->bindMobile($mobile, $smsCode, $openid, $unionid, $nickname, $avatar);
            return $this->success(['token'=>$token]);
        }catch (OilcardException $e){
            return $this->fail(ErrorCode::ERROR,$e->getMessage());
        }catch (\Exception $e){
            return $this->fail(ErrorCode::ERROR,"系统错误");
        }
    }

    public function getUser(){
        try {
            return $this->success($this->userInfo);
        }catch (OilcardException $e){
            return $this->fail(ErrorCode::ERROR,$e->getMessage());
        }catch (\Exception $e){
            return $this->fail(ErrorCode::ERROR,"系统错误");
        }
    }
}